//
//  WuLiuViewController.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-10-2.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "WuLiuViewController.h"
#import "WuLiuCell.h"
@interface WuLiuViewController ()

@end

@implementation WuLiuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([_type isEqualToString:@"wuliu"]) {
        self.titleLabel.text = @"查看物流";
        dataArray = [[NSMutableArray alloc] initWithArray:@[@"已发货",@"到达北京市朝阳区物流中心",@"正在派件",@"已收货"]];
    }else{
        self.titleLabel.text = @"订单记录";
        dataArray = [[NSMutableArray alloc] init];
    }

    

    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:myTableView];

    header = [self createHeader];

    myTableView.tableHeaderView = header;
    
    if ([_type isEqualToString:@"wuliu"]) {
        
    }else{
        [self getDataWithDict:@{@"OrderID":_orderID} api:API_ORDERRECORDER];
    }
}

-(void)getDataWithDict:(NSDictionary *)dict api:(NSString *)api
{
    
    if (![XDTools NetworkReachable]) {
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    
    __weak ASIHTTPRequest *request = [XDTools postRequestWithDict:dict API:api];
    [request setCompletionBlock:^{
        
        [XDTools hideProgress:self.contentView];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        
        if([[tempDic objectForKey:@"status"]intValue] == 1){
            
            DDLOG(@"dic = %@",tempDic);
            
            [dataArray addObjectsFromArray:tempDic[@"data"]];
            
            [myTableView reloadData];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
            
        }
        
    }];
    
    [request setFailedBlock:^{
        [XDTools hideProgress:self.contentView];
    }];
    [request startAsynchronous];
    [XDTools showProgress:self.contentView];
}

- (UIView *)createHeader{
    UIView * head = [UIView initViewRect:CGRectMake(0, 0, UI_SCREEN_WIDTH, 95) andBackGroudColor:[UIColor clearColor]];

    
    NSArray * leftArray;
    if ([_type isEqualToString:@"wuliu"]) {
        leftArray = @[@"配送方式:",@"收  货  人:",@"订单编号:"];
    }else{
        leftArray = @[@"支付方式:",@"摄  影  师:",@"订单编号:"];
    }
    NSArray * rightArray = @[@"在线支付",_cameraManName,_orderCode];

    for (int i = 0; i < 3; i++) {
        UILabel * leftLB = [UILabel initLabelText:leftArray[i] andRect:CGRectMake(0, 10+25*i, 90, 25) andBackGroudColor:[UIColor clearColor] andTextFont:[UIFont systemFontOfSize:15] andTextColor:RGBA(154, 154, 154, 1) andTextAlignment:NSTextAlignmentRight];
        [head addSubview:leftLB];


        UILabel * rightLB = [UILabel initLabelText:rightArray[i] andRect:CGRectMake(VIEW_POINT_MAX_X(leftLB)+10, 10+25*i, 200, 25) andBackGroudColor:[UIColor clearColor] andTextFont:[UIFont systemFontOfSize:15] andTextColor:RGBA(154, 154, 154, 1) andTextAlignment:NSTextAlignmentLeft];
        [head addSubview:rightLB];

    }

    UIView * line = [[XDTools sharedXDTools] createSeparatorLineWithRect:CGRectMake(0,94.5f, UI_SCREEN_WIDTH, .5f)];
    [head addSubview:line];

    return head;
}


#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65 ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    WuLiuCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
    if (!cell) {
        cell = [[WuLiuCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (!indexPath.row) {
        cell.leftIV.image = [UIImage imageNamed:@"wuliu01"];
    }else{
        cell.leftIV.image = [UIImage imageNamed:@"wuliu02"];
    }
    cell.titleLB.text = dataArray[indexPath.row][@"Oper"];
    cell.subTitleLB.text = dataArray[indexPath.row][@"OperTime"];

    return cell;

}

#pragma mark - TableView Delegate



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
